import axios from "../axios";
// import { Toast } from 'vant';

let tool = {
  config: null
};

function getConfig() {
  let ua = navigator.userAgent.toLowerCase();
  let isWeixin = ua.indexOf('micromessenger') > -1;
  if (isWeixin) {
    return new Promise((succ, err) => {
      if (tool.config) {
        window.wx.config({ ...tool.config, debug: false, jsApiList: [ 'scanQRCode', 'chooseImage', 'uploadImage', 'getLocation', 'openLocation' ] });
        succ();
      } else {
        axios.get('/api/login/window.wxJsSdkConfig', {
          url: window.location.href.split('#')[0]
        }).then(res => {
          tool.config = res || {};
          window.wx.config({ ...tool.config, debug: false, jsApiList: [ 'scanQRCode', 'chooseImage', 'uploadImage', 'getLocation', 'openLocation' ] });
          succ();
        }).catch(() => {
          err();
        });
      }
    });
  } else {
    return Promise.reject();
  }
}

tool.scanEquipment = function() {
  return new Promise((succ, err) => {
    getConfig().then(() => {
      window.wx.scanQRCode({
        needResult: 1, 
        scanType: [ "qrCode" ], 
        success: res => {
          let result = res.resultStr; 
          if (result.indexOf('dianxiaotong') > -1) {
            let sn = result.split('=')[1];
            if (isNaN( sn[0] )) {
              axios.get('/api/chargerPlaEquipmentController/getSnById', {
                id: result.split('=')[1]
              }).then(res => {
                succ(res);
              });
            } else {
              succ(sn);
            }
          }
        },
        error: e => err(e)
      });
    });
  });
}

tool.scanEquipmentOrSn = function() {
  return new Promise((succ, err) => {
    getConfig().then(() => {
      window.wx.scanQRCode({
        needResult: 1, 
        scanType: [ "qrCode" ], 
        success: res => {
          let code = res.resultStr; 
          if (code.indexOf('dianxiaotong') > -1) {
            let sn = code.split('=')[1];
            if (isNaN( sn[0] )) {
              axios.get('/api/chargerPlaEquipmentController/getSnById', {
                id: code.split('=')[1]
              }).then(res => {
                succ({ code: res, type: 1 });
              });
            } else {
              succ({ code: sn, type: 1 });
            }
          } else {
            if (code.indexOf('=') > 0) {
              succ({ code: code.split('=')[1], type: 2 });
            } else {
              succ({ code, type: 2 });
            }
          }
        },
        error: e => err(e)
      });
    });
  });
}

tool.seleImg = function() {
  return new Promise((succ, err) => {
    getConfig().then(() => {
      window.wx.chooseImage({ 
        count: 1, 
        sizeType: [ 'compressed' ], 
        sourceType: [ 'album', 'camera' ], 
        success: res => {        
          window.wx.uploadImage({ 
            localId: res.localIds[ res.localIds.length - 1 ], 
            isShowProgressTips: 1,
            success: res => {
              axios.get('/api/upload/getUploadFile', {
                media_id: res.serverId
              }).then(res => {
                succ(res);
              });
            }
          });
        },
        error: e => err(e)
      });
    });
  });
}

export default tool;